<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="标题" prop="navtitle">
        <el-input v-model="dataForm.navtitle" placeholder="" />
      </el-form-item>
      <el-form-item label="网址" prop="navurl">
        <el-input v-model="dataForm.navurl" placeholder="" />
      </el-form-item>
      <el-form-item label="导航类型" prop="navtype">
        <!-- <el-input v-model="dataForm.isshow" placeholder=""></el-input> -->
        <el-select v-model="dataForm.navtype" style="width: 100%" placeholder="请选择">
          <el-option
            v-for="item in navtypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      <!-- <el-input v-model="dataForm.navtype" placeholder=""></el-input> -->
      </el-form-item>
      <el-form-item label="是否显示" prop="isshow">
        <el-select v-model="dataForm.isshow" style="width: 100%" placeholder="请选择">
          <el-option
            v-for="item in isshows"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="打开方式" prop="isopen">
        <el-select v-model="dataForm.isopen" style="width: 100%" placeholder="请选择">
          <el-option
            v-for="item in openwaies"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      <!-- <el-input v-model="dataForm.isopen" placeholder=""></el-input> -->
      </el-form-item>
      <el-form-item label="排序号" prop="navsort">
        <el-input v-model="dataForm.navsort" placeholder="" />
      </el-form-item>
    </el-form>
    <el-row>
      <el-col style="text-align: right;">
        <el-button size="mini" icon="el-icon-close" @click="visible = false">取消</el-button>
        <el-button size="mini" icon="el-icon-check" type="primary" @click="dataFormSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        navtype: '',
        navtitle: '',
        navurl: '',
        isshow: '',
        isopen: '',
        navsort: '',
        createtime: ''
      },
      navtypes: [{
        value: 0,
        label: '顶部'
      }, {
        value: 1,
        label: '底部'
      }],
      isshows: [{
        value: 1,
        label: '是'
      }, {
        value: 0,
        label: '否'
      }],
      openwaies: [{
        value: 0,
        label: '页面跳转'
      }, {
        value: 1,
        label: '新窗口打开'
      }],
      dataRule: {
        navtype: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        navtitle: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        navurl: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        isshow: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        isopen: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        navsort: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        createtime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/navs/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.navtype = data.navs.navtype
              this.dataForm.navtitle = data.navs.navtitle
              this.dataForm.navurl = data.navs.navurl
              this.dataForm.isshow = data.navs.isshow
              this.dataForm.isopen = data.navs.isopen
              this.dataForm.navsort = data.navs.navsort
              this.dataForm.createtime = data.navs.createtime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/navs/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'navtype': this.dataForm.navtype,
              'navtitle': this.dataForm.navtitle,
              'navurl': this.dataForm.navurl,
              'isshow': this.dataForm.isshow,
              'isopen': this.dataForm.isopen,
              'navsort': this.dataForm.navsort,
              'createtime': this.dataForm.createtime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
